//
//  loginViewController.m
//  pb
//
//  Created by 千翼网络科技 on 2017/5/5.
//  Copyright © 2017年 qianyi. All rights reserved.
//

#import "loginViewController.h"
#import "planDetailsViewController.h"

@interface loginViewController ()<UITextFieldDelegate>

@property (nonatomic,strong) UITextField *accountTextField;
@property (nonatomic,strong) UITextField *passwordTextField;

@end

@implementation loginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = getColor(@"ffffff");
    [self initView];
}

#pragma mark --------------------- init --------------------

- (void)initView{

    UIImageView *logoImg = [[UIImageView alloc]init];
    logoImg.image = [UIImage imageNamed:@"logo"];
    [self.view addSubview:logoImg];
    [logoImg mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(self.view).with.offset(screenScale(190));
        make.left.mas_equalTo(self.view).with.offset( (scrWidth() - screenScale(170))/2);
        make.width.mas_equalTo(screenScale(170));
        make.height.mas_equalTo(screenScale(170));
    }];
    
    UILabel *nameLabel = [[UILabel alloc]init];
    nameLabel.text = @"掌上南航";
    nameLabel.textColor = getColor(@"000000");
    nameLabel.font = get4sFont(16);
    nameLabel.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:nameLabel];
    [nameLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(logoImg.mas_bottom).with.offset(screenScale(30));
        make.left.mas_equalTo(self.view).with.offset(0);
        make.width.mas_equalTo(scrWidth());
        make.height.mas_equalTo(screenScale(34));
    }];
    
    // 账号
    
    _accountTextField = [[UITextField alloc]init];
    _accountTextField.borderStyle = UITextBorderStyleNone;
    _accountTextField.tintColor = [UIColor blackColor];
    _accountTextField.delegate = self;
    _accountTextField.backgroundColor = getColor(@"ffffff");
    _accountTextField.clearButtonMode = UITextFieldViewModeAlways;
    _accountTextField.returnKeyType = UIReturnKeyNext;
    _accountTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入工牌号码" attributes:@{NSForegroundColorAttributeName: getColor(@"cecece")}];
    _accountTextField.font = get4sFont(12);
    [self.view addSubview:_accountTextField];
    [_accountTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(nameLabel.mas_bottom).with.offset(screenScale(105));
        make.left.mas_equalTo(self.view).with.offset(screenScale(60));
        make.width.mas_equalTo(scrWidth() - screenScale(120));
        make.height.mas_equalTo(screenScale(50));
    }];
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenScale(50), screenScale(50))];
    UIImageView *leftView = [[UIImageView alloc]initWithFrame:CGRectMake(screenScale(8), screenScale(8), screenScale(34), screenScale(34))];
    leftView.image = [UIImage imageNamed:@"code"];
    [view addSubview:leftView];
    _accountTextField.leftView = view;
    _accountTextField.leftViewMode = UITextFieldViewModeAlways;
    
    UIView *threadView = [[UIView alloc]init];
    threadView.backgroundColor = getThreadColor();
    [_accountTextField addSubview:threadView];
    [threadView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_accountTextField.mas_bottom).with.offset(0);
        make.left.mas_equalTo(_accountTextField).with.offset(0);
        make.width.mas_equalTo(scrWidth() - screenScale(120));
        make.height.mas_equalTo(screenScale(1));
    }];
    
    // 密码
    
    _passwordTextField = [[UITextField alloc]init];
    _passwordTextField.borderStyle = UITextBorderStyleNone;
    _passwordTextField.tintColor = [UIColor blackColor];
    _passwordTextField.delegate = self;
    _passwordTextField.backgroundColor = getColor(@"ffffff");
    _passwordTextField.clearButtonMode = UITextFieldViewModeAlways;
    _passwordTextField.returnKeyType = UIReturnKeyGo;
    _passwordTextField.secureTextEntry = YES;
    _passwordTextField.attributedPlaceholder = [[NSAttributedString alloc] initWithString:@"请输入密码" attributes:@{NSForegroundColorAttributeName: getColor(@"cecece")}];
    _passwordTextField.font = get4sFont(12);
    [self.view addSubview:_passwordTextField];
    [_passwordTextField mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_accountTextField.mas_bottom).with.offset(screenScale(60));
        make.left.mas_equalTo(self.view).with.offset(screenScale(60));
        make.width.mas_equalTo(scrWidth() - screenScale(120));
        make.height.mas_equalTo(screenScale(50));
    }];
    
    UIView *view1 = [[UIView alloc]initWithFrame:CGRectMake(0, 0, screenScale(50), screenScale(50))];
    UIImageView *leftView1 = [[UIImageView alloc]initWithFrame:CGRectMake(screenScale(8), screenScale(8), screenScale(34), screenScale(34))];
    leftView1.image = [UIImage imageNamed:@"x_code1"];
    [view1 addSubview:leftView1];
    _passwordTextField.leftView = view1;
    _passwordTextField.leftViewMode = UITextFieldViewModeAlways;
    
    UIView *threadView1 = [[UIView alloc]init];
    threadView1.backgroundColor = getThreadColor();
    [_passwordTextField addSubview:threadView1];
    [threadView1 mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_passwordTextField.mas_bottom).with.offset(0);
        make.left.mas_equalTo(_passwordTextField).with.offset(0);
        make.width.mas_equalTo(scrWidth() - screenScale(120));
        make.height.mas_equalTo(screenScale(1));
    }];
    
    UIButton *loginBtn = [[UIButton alloc]init];
    loginBtn.backgroundColor = getMainColor();
    loginBtn.layer.cornerRadius = screenScale(10);
    loginBtn.layer.masksToBounds = YES;
    loginBtn.titleLabel.font = get4sFont(14);
    [loginBtn setTitle:@"登录" forState:UIControlStateNormal];
    [loginBtn addTarget:self action:@selector(loginBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:loginBtn];
    [loginBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.mas_equalTo(_passwordTextField.mas_bottom).with.offset(screenScale(88));
        make.left.mas_equalTo(_passwordTextField).with.offset(0);
        make.width.mas_equalTo(scrWidth() - screenScale(120));
        make.height.mas_equalTo(screenScale(77));
    }];
    
}

#pragma mark --------------------- click --------------------

- (void)loginBtnClick{
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:[[UIApplication sharedApplication]keyWindow] animated:YES];
    hud.tag = 500;
    //提示文本
    hud.labelText = @"正在加载...";
    //是否有遮罩
    hud.dimBackground = YES;
    
    NSMutableDictionary *productParams = [[NSMutableDictionary alloc]init];
    [productParams setValue: _accountTextField.text forKey:@"number"];
    [productParams setValue: _passwordTextField.text forKey:@"pass"];

//    [productParams setValue: @"123456" forKey:@"number"];
//    [productParams setValue: @"123456" forKey:@"pass"];
    
    [[MNetworkHandler sharedInstance]conURL:Login_URL
                                networkType:NetWorkPOST
                                     params:productParams
                                   delegate:nil
                                    showHUD:NO
                               successBlock:^(id returnData) {
                                   if ([[returnData objectForKey:@"code"]intValue] == 1) {
                                       
                                       [hud removeFromSuperview];
                                       
                                       [PublicFunction saveConfig:_accountTextField.text forKey:@"account"];
                                       [PublicFunction saveConfig:returnData[@"data"][@"token"] forKey:@"token"];
                                       [PublicFunction saveConfig:returnData[@"data"][@"token"] forKey:@"member_id"];
                                       
                                       MyTabBarController *barController = [[MyTabBarController alloc] init];
                                       barController.view.backgroundColor = getColor(@"ffffff");
                                       UINavigationBar *bar = [UINavigationBar appearance];
                                       bar.barStyle=UIBarStyleBlackTranslucent;
                                       //设置显示的颜色
                                       bar.barTintColor = getMainColor();
                                       //设置字体颜色
                                       bar.tintColor = [UIColor whiteColor];
                                       [bar setTitleTextAttributes:@{NSForegroundColorAttributeName : [UIColor whiteColor]}];
                                       
                                       [self presentViewController:barController animated:YES completion:nil];
                                       
                                   }else{
                                       
                                       [hud removeFromSuperview];
                                       [PublicFunction showAlert:nil message:[returnData objectForKey:@"msg"] duration:1.0f];
                                       
                                   }
                               } failureBlock:^(NSError *error) {
                                   NSLog(@"%@",error);
                                   [hud removeFromSuperview];
                                   [PublicFunction showAlert:nil message:@"没有连接网络" duration:1.0f];
                               }];
}

- (BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if (textField == _accountTextField) {
        
        [_passwordTextField becomeFirstResponder];
        
    }else if (textField == _passwordTextField){
    
        [self loginBtnClick];
    }
    return YES;
}



@end
